<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.Constants"%>
<%@page import="Utilities.Constants.LogsRetrievalLimit"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Beans.Requests.DataRetrieval.LogsDataRetrievalRequest"%>
<%@page import="Beans.Responses.DataRetrievalComponents.LogDataComponent"%>
<%@page import="Beans.Responses.DataRetrievalResponse"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Managers.Utilities.User"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);

    //data retrieval request and response
    LogsDataRetrievalRequest dataRequest = null;
    DataRetrievalResponse<LogDataComponent> data = null;

    //retrieves the logs retrieval limit
    String limit = request.getParameter("limit");
    //retrieves logs retrieval severity
    String severityType = request.getParameter("severity");
    if(limit != null)
    {
        //builds the request object based on the specified limit
        if(limit.equalsIgnoreCase("severity"))
        {
            dataRequest = new LogsDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                       request.getParameter("severity"));
        }
        else if(limit.equalsIgnoreCase("server"))
        {
            int serverId = Constants.INVALID_SERVER_ID;

            try
            {
                serverId = Integer.parseInt(request.getParameter("serverId"));
            }
            catch(NumberFormatException e){}

            dataRequest = new LogsDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                       serverId,
                                                       LogsRetrievalLimit.SERVER_ID);
        }
        else if(limit.equalsIgnoreCase("initiator"))
        {
            int initiatorId = Constants.INVALID_SERVER_ID;

            try
            {
                initiatorId = Integer.parseInt(request.getParameter("initiatorId"));
            }
            catch(NumberFormatException e){}

            dataRequest = new LogsDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                       initiatorId,
                                                       LogsRetrievalLimit.INITIATOR_ID);
        }
        else
            ;
    }
    else //no limit was specified
        dataRequest = new LogsDataRetrievalRequest(userSessionData.getUserData().getUserId());

    //attempts to submit the request and retrieves the response
    if(UIFunctions.sendServiceRequest(request, response, dataRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
        data = (DataRetrievalResponse<LogDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
    else
        ;
%>
<p:printPageHeader pageName="Logs" pageId="pageLogs" userSessionData="<%= userSessionData %>"/>
    <ul class="navigationBar">
        <li <%= (severityType != null && severityType.equals("D")) ? "id='currentSub'" : "" %>>
            <a href="logs.jsp?limit=severity&amp;severity=D">Debug</a></li>
        <li <%= (severityType != null && severityType.equals("I")) ? "id='currentSub'" : "" %>>
            <a href="logs.jsp?limit=severity&amp;severity=I">Information</a>
        <li <%= (severityType != null && severityType.equals("L")) ? "id='currentSub'" : "" %>>
            <a href="logs.jsp?limit=severity&amp;severity=L">Login</a></li>
        <li <%= (severityType != null && severityType.equals("W")) ? "id='currentSub'" : "" %>>
            <a href="logs.jsp?limit=severity&amp;severity=W">Warning</a></li>
        <li <%= (severityType != null && severityType.equals("E")) ? "id='currentSub'" : "" %>>
            <a href="logs.jsp?limit=severity&amp;severity=E">Error</a></li>
        <li <%= (severityType == null) ? "id='currentSub'" : "" %>>
            <a href="logs.jsp">Display All</a></li>
    </ul>
    <p:printLogs data="<%= data %>"/>
<p:printPageFooter/>